package database;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

public class UIDManger {
	private static final String PATH = "Database/UID.xml";
	private static final String PATH_BID = "Database/BID.xml";
	private static final String PATH_CID = "Database/CID.xml";
	private static final String PATH_PID = "Database/PID.xml";
	private static final String PATH_PICID = "Database/PICID.xml";
	private static final String PATH_MOID = "Database/MOID.xml";
	private static final String PATH_SOID = "Database/SOID.xml";
	
	public static String getValiUID() throws Exception {
		final String DEFAULT = "This is the last UID taked.";
		final String FLAG = "uid";
		Properties propUID = new Properties();
		
		int uid = 0;
		
		File file = new File(PATH);
		PrepareFiles.prepareFile((PATH), DEFAULT);
		FileInputStream in = new FileInputStream(file);
		propUID.loadFromXML(in);
		in.close();
		String lastUID = propUID.getProperty(FLAG);
		if(lastUID == null) {
			lastUID = "1000";
		}
		
		try {
			uid = Integer.parseInt(lastUID);
		} catch (Exception ex) {
			throw new Exception("It does not seems to be a valid UID file.");
		}
		
		propUID.setProperty(FLAG, Integer.toString(++uid));
		FileOutputStream out = new FileOutputStream(file);
		propUID.storeToXML(out, DEFAULT);
		out.close();
		return propUID.getProperty(FLAG);
	}
	
	public static String getValidBlgoID() throws Exception {
		final String DEFAULT = "This is the last BID taked.";
		final String FLAG = "bid";
		Properties propBID = new Properties();
		
		int uid = 0;
		
		File file = new File(PATH_BID);
		PrepareFiles.prepareFile((PATH_BID), DEFAULT);
		FileInputStream in = new FileInputStream(file);
		propBID.loadFromXML(in);
		in.close();
		String lastUID = propBID.getProperty(FLAG);
		if(lastUID == null) {
			lastUID = "10000";
		}
		
		try {
			uid = Integer.parseInt(lastUID);
		} catch (Exception ex) {
			throw new Exception("It does not seems to be a valid BID file.");
		}
		
		propBID.setProperty(FLAG, Integer.toString(++uid));
		FileOutputStream out = new FileOutputStream(file);
		propBID.storeToXML(out, DEFAULT);
		out.close();
		return propBID.getProperty(FLAG);
	} 
	
	public static String getValidCommentID() throws Exception {
		final String DEFAULT = "This is the last CID taked.";
		final String FLAG = "cid";
		Properties propCID = new Properties();
		
		int cid = 0;
		
		File file = new File(PATH_CID);
		PrepareFiles.prepareFile(PATH_CID, DEFAULT);
		FileInputStream in = new FileInputStream(file);
		propCID.loadFromXML(in);
		in.close();
		String lastCID = propCID.getProperty(FLAG);
		if(lastCID == null) {
			lastCID = "100000";
		}
		
		try {
			cid = Integer.parseInt(lastCID);
		} catch (Exception ex) {
			throw new Exception("It does not seems to be a valid CID file.");
		}
		
		propCID.setProperty(FLAG, Integer.toString(++cid));
		FileOutputStream out = new FileOutputStream(file);
		propCID.storeToXML(out, DEFAULT);
		out.close();
		return propCID.getProperty(FLAG);
	}
	
	public static String getValidPostID() throws Exception {
		final String DEFAULT = "This is the last PID taked.";
		final String FLAG = "pid";
		Properties propPID = new Properties();
		
		int pid = 0;
		
		File file = new File(PATH_PID);
		PrepareFiles.prepareFile(PATH_PID, DEFAULT);
		FileInputStream in = new FileInputStream(file);
		propPID.loadFromXML(in);
		in.close();
		String lastPID = propPID.getProperty(FLAG);
		if(lastPID == null) {
			lastPID = "1000000";
		}
		
		try {
			pid = Integer.parseInt(lastPID);
		} catch (Exception ex) {
			throw new Exception("It does not seems to be a valid PID file.");
		}
		
		propPID.setProperty(FLAG, Integer.toString(++pid));
		FileOutputStream out = new FileOutputStream(file);
		propPID.storeToXML(out, DEFAULT);
		out.close();
		return propPID.getProperty(FLAG);
	}
	
	public static String getValidPictureID() throws Exception {
		final String DEFAULT = "This is the last PICID taked.";
		final String FLAG = "picid";
		Properties propPICID = new Properties();
		
		int picid = 0;
		
		File file = new File(PATH_PICID);
		PrepareFiles.prepareFile(PATH_PICID, DEFAULT);
		FileInputStream in = new FileInputStream(file);
		propPICID.loadFromXML(in);
		in.close();
		String lastPICID = propPICID.getProperty(FLAG);
		if(lastPICID == null) {
			lastPICID = "10000000";
		}
		
		try {
			picid = Integer.parseInt(lastPICID);
		} catch (Exception ex) {
			throw new Exception("It does not seems to be a valid PICID file.");
		}
		
		propPICID.setProperty(FLAG, Integer.toString(++picid));
		FileOutputStream out = new FileOutputStream(file);
		propPICID.storeToXML(out, DEFAULT);
		out.close();
		return propPICID.getProperty(FLAG);
	}
	
	public static String getValidMovieID() throws Exception {
		final String DEFAULT = "This is the last MOID taked.";
		final String FLAG = "moid";
		Properties propMOID = new Properties();
		
		int moid = 0;
		
		File file = new File(PATH_MOID);
		PrepareFiles.prepareFile(PATH_MOID, DEFAULT);
		FileInputStream in = new FileInputStream(file);
		propMOID.loadFromXML(in);
		in.close();
		String lastMOID = propMOID.getProperty(FLAG);
		if(lastMOID == null) {
			lastMOID = "10000000";
		}
		
		try {
			moid = Integer.parseInt(lastMOID);
		} catch (Exception ex) {
			throw new Exception("It does not seems to be a valid MOID file.");
		}
		
		propMOID.setProperty(FLAG, Integer.toString(++moid));
		FileOutputStream out = new FileOutputStream(file);
		propMOID.storeToXML(out, DEFAULT);
		out.close();
		return propMOID.getProperty(FLAG);
	}
	
	public static String getValidSundID() throws Exception {
		final String DEFAULT = "This is the last SOID taked.";
		final String FLAG = "soid";
		Properties propSOID = new Properties();
		
		int soid = 0;
		
		File file = new File(PATH_SOID);
		PrepareFiles.prepareFile(PATH_SOID, DEFAULT);
		FileInputStream in = new FileInputStream(file);
		propSOID.loadFromXML(in);
		in.close();
		String lastSOID = propSOID.getProperty(FLAG);
		if(lastSOID == null) {
			lastSOID = "10000000";
		}
		
		try {
			soid = Integer.parseInt(lastSOID);
		} catch (Exception ex) {
			throw new Exception("It does not seems to be a valid MOID file.");
		}
		
		propSOID.setProperty(FLAG, Integer.toString(++soid));
		FileOutputStream out = new FileOutputStream(file);
		propSOID.storeToXML(out, DEFAULT);
		out.close();
		return propSOID.getProperty(FLAG);
	}
}